package com.mxpio.erp.equipment.service.impl;

import com.mxpio.erp.common.enums.CommonEnums;
import com.mxpio.erp.common.equipment.entity.Equipment;
import com.mxpio.erp.common.equipment.entity.EquipmentAttr;
import com.mxpio.erp.common.equipment.entity.EquipmentEqpAttr;
import com.mxpio.erp.common.equipment.entity.EquipmentEqpBasics;
import com.mxpio.erp.common.equipment.service.EquipmentService;
import com.mxpioframework.common.util.BeanReflectionUtils;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.JpaUtil;
import com.mxpioframework.jpa.lin.Linu;
import com.mxpioframework.system.service.impl.BaseServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service
public class EquipmentServiceImpl extends BaseServiceImpl<Equipment> implements EquipmentService {

    @Override
    @Transactional
    public Result<Equipment> editAndSync(Equipment equipment) {
        this.update(equipment);
        //同步更新设备台账
        Linu linu = JpaUtil.linu(EquipmentEqpBasics.class);
        linu.set("eqpName",equipment.getEqpName());//设备名称
        linu.set("eqpTypeId",equipment.getEqpTypeCode());//设备分类编码
        linu.set("specType",equipment.getSpecType());//规格型号
        linu.set("power",equipment.getPower());//设备功率
        linu.set("manufacturer",equipment.getManufacturer());//制造商
        linu.set("isKey",equipment.getIsKey());//是否关键设备
        linu.set("isEsa",equipment.getIsEsa());//是否ESA设备
        linu.set("isDigital",equipment.getIsDigital());//是否数控设备
        linu.set("digitalControl",equipment.getDigitalControl());//数控系统
        linu.set("isSubOrdinate",equipment.getIsSubOrdinate());//是否附属设备
        linu.set("isCalibrate",equipment.getIsCalibrate());//是否需要检定
        linu.set("calibrateCycle",equipment.getCalibrateCycle());//检定周期(天)
        linu.set("calibrateAdvance",equipment.getCalibrateAdvance());//检定提前期(天) 检定任务提前生成天数
        linu.set("eqpClass",equipment.getEqpClass());
        linu.equal("eqpCode",equipment.getEqpCode());
        linu.update();//执行更新
        return Result.OK(equipment);
    }
    @Override
    @Transactional
    public Result<String> batchSyncEqpBasics(List<String> eqpCodeList) {
        Map<String, Equipment> eqpMap = null;
        if (eqpCodeList==null||eqpCodeList.isEmpty()){//支持空入参的全量设备更新
            //return Result.error("入参为空");
            List<Equipment> allEq = JpaUtil.linq(Equipment.class).list();
            eqpMap = JpaUtil.index(allEq, "eqpCode");
            eqpCodeList = allEq.stream().map(Equipment::getEqpCode).collect(Collectors.toList());
        }
        if (eqpCodeList==null||eqpCodeList.isEmpty()){
            return Result.error("设备信息为空");
        }
        for (String eqpCode:eqpCodeList){
            Equipment equipment = null;
            if (eqpMap!=null&&eqpMap.containsKey(eqpCode)){
                equipment = eqpMap.get(eqpCode);
            }else{
                equipment = getById(Equipment.class, eqpCode);
            }
            if (equipment==null){
                continue;
            }
//            List<EquipmentEqpBasics> list = JpaUtil.linq(EquipmentEqpBasics.class).equal("eqpCode", eqpCode).list();
//            EquipmentEqpBasics one = JpaUtil.linq(EquipmentEqpBasics.class).equal("eqpCode", eqpCode).findOne();
//            if (one.getIsRenewal().equals(CommonEnums.YesNo.YES.getCode()))
            Linu linu = JpaUtil.linu(EquipmentEqpBasics.class);
            linu.set("eqpName",equipment.getEqpName());//设备名称
            linu.set("eqpTypeId",equipment.getEqpTypeCode());//设备分类编码
            linu.set("specType",equipment.getSpecType());//规格型号
            linu.set("power",equipment.getPower());//设备功率
            linu.set("manufacturer",equipment.getManufacturer());//制造商
            linu.set("isKey",equipment.getIsKey());//是否关键设备
            linu.set("isEsa",equipment.getIsEsa());//是否ESA设备
            linu.set("isDigital",equipment.getIsDigital());//是否数控设备
            linu.set("digitalControl",equipment.getDigitalControl());//数控系统
            linu.set("isSubOrdinate",equipment.getIsSubOrdinate());//是否附属设备
            linu.set("isCalibrate",equipment.getIsCalibrate());//是否需要检定
            linu.set("calibrateCycle",equipment.getCalibrateCycle());//检定周期(天)
            linu.set("calibrateAdvance",equipment.getCalibrateAdvance());//检定提前期(天) 检定任务提前生成天数

            linu.set("inspectDutyUser",equipment.getInspectDutyUser());//检定负责人
            linu.set("repairPersonGroupCode",equipment.getRepairPersonGroupCode());//维修群组
            linu.set("upkeepPersonGroupCode",equipment.getUpkeepPersonGroupCode());//保养群组
            linu.set("engineerPersonGroupCode",equipment.getEngineerPersonGroupCode());//工程师组

            linu.set("eqpClass",equipment.getEqpClass());

            linu.equal("eqpCode",equipment.getEqpCode());
            linu.update();//执行更新

            //更新设备台账档案
            List<EquipmentEqpBasics> eqpBasics = JpaUtil.linq(EquipmentEqpBasics.class).equal("eqpCode", equipment.getEqpCode()).list();
            List<EquipmentEqpAttr> syncAddList = new ArrayList<>();
            if(!eqpBasics.isEmpty()){
                for (EquipmentEqpBasics basics:eqpBasics){
                    if (basics.getIsRenewal().equals(CommonEnums.YesNo.YES.getCode())){
                        JpaUtil.lind(EquipmentEqpAttr.class)
                                .equal("basicsCode",basics.getBasicsCode())
                                .equal("isSync",CommonEnums.YesNo.YES.getCode())
                                .delete();//删除历史同步的
                        List<EquipmentAttr> attrList = JpaUtil.linq(EquipmentAttr.class).equal("eqpCode", eqpCode).list();
                        if (attrList.size()>0){
                            for (EquipmentAttr attr:attrList){
                                EquipmentEqpAttr eqpAttr = new EquipmentEqpAttr();
                                BeanReflectionUtils.copyProperties(eqpAttr,attr);
                                eqpAttr.setBasicsCode(basics.getBasicsCode());
                                eqpAttr.setId(null);
                                eqpAttr.setIsSync(CommonEnums.YesNo.YES.getCode());//档案信息由设备同步而来
                                syncAddList.add(eqpAttr);
                            }
                        }
                    }
                }
            }
            if(!syncAddList.isEmpty()){
                JpaUtil.save(syncAddList);//保存到设备台账 档案
            }
        }
        return Result.OK();
    }
}
